package com.example.smalldimplestore.view;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.smalldimplestore.R;
import com.example.smalldimplestore.databinding.WidgetMainTableViewBinding;

/**
 * Created by 86178 on 2019/11/24.
 * author mogen
 * 用心，成就一切。
 */


public class MainTableView extends LinearLayout implements View.OnClickListener {
    private WidgetMainTableViewBinding binding;
    private OnTableChangedListener OnTableChangedListener;
    private int lastIndex = 0;
    private ImageView imgs[] = new ImageView[3];
    private int defaultImgRes[] = new int[3];
    private int selectImgRes[] = new int[3];
    private TextView tvModuleTexts[] = new TextView[3];
    private Context mcontext;

    public MainTableView(Context context) {
        this(context, null);
    }

    public MainTableView(Context context, AttributeSet attrs) {
        super(context, attrs);
        binding = DataBindingUtil.inflate( LayoutInflater.from(context), R.layout.widget_main_table_view, this, true);
        binding.setPresenter(this);

        mcontext = context;

        imgs[0] = binding.ivModuleFirst;
        imgs[1] = binding.ivModuleQuanZi;
        imgs[2] = binding.ivModuleClass;

        tvModuleTexts[0] = binding.tvModuleFirst;
        tvModuleTexts[1] = binding.tvModuleSecond;
        tvModuleTexts[2] = binding.tvModuleThird;

        defaultImgRes[0] = R.drawable.icon_firse_page_point_before;
        selectImgRes[0] = R.drawable.icon_firse_page_point_afpter;
        defaultImgRes[1] = R.drawable.icon_second_page_point_before;
        selectImgRes[1] = R.drawable.icon_second_page_point_after;
        defaultImgRes[2] = R.drawable.icon_third_page_point_before;
        selectImgRes[2] = R.drawable.icon_third_page_point_after;

        binding.llModuleFirst.setOnClickListener(this);
        binding.llModuleSecond.setOnClickListener(this);
        binding.llModuleThird.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llModuleFirst:
                onClick(0);
                break;
            case R.id.llModuleSecond:
                onClick(1);
                break;
            case R.id.llModuleThird:
                onClick(2);
                break;
            default:
        }
    }

    /**
     * 手动触发点击事件
     */
    public void onClick(int index) {
        if (lastIndex == index) {
            return;
        }
        //更改底部点击图标的方法
        imgs[lastIndex].setImageResource(defaultImgRes[lastIndex]);
        imgs[index].setImageResource(selectImgRes[index]);
        for (TextView tv : tvModuleTexts) {
            tv.setTextColor(mcontext.getResources().getColor( R.color.tv_2));
        }
        tvModuleTexts[index].setTextColor(mcontext.getResources().getColor(R.color.red));
        lastIndex = index;
        if (OnTableChangedListener != null) {
            OnTableChangedListener.onTableChanged(index);
        }
    }

    public void setOnTableChangedListener(OnTableChangedListener onTableChangedListener) {
        this.OnTableChangedListener = onTableChangedListener;
    }


    public interface OnTableChangedListener {
        void onTableChanged(int index);
    }

}

